<?php

declare(strict_types=1);

namespace app\validate\system;

use think\Validate;

class SysConfigValidate extends Validate
{
    protected $rule = [
        'config_key' => ['require', 'max:120', 'regex' => '/^[A-Za-z0-9_\-\.]+$/'],
        'config_value' => 'require',
        'value_type' => 'in:string,json,bool,int,decimal,float',
        'description' => 'max:255',
        'category' => 'max:60',
        'status' => 'in:0,1',
    ];

    protected $message = [
        'config_key.require' => '配置键不能为空',
        'config_key.max' => '配置键长度不能超过120字符',
        'config_key.regex' => '配置键只能包含字母、数字、下划线、短横线或点号',
        'config_value.require' => '配置值不能为空',
        'value_type.in' => '配置值类型不正确',
        'description.max' => '描述长度不能超过255字符',
        'category.max' => '分类长度不能超过60字符',
        'status.in' => '状态值不正确',
    ];

    protected $scene = [
        'create' => ['config_key', 'config_value', 'value_type', 'description', 'category', 'status'],
        'update' => ['config_value', 'value_type', 'description', 'category', 'status'],
    ];

    protected function sceneCreate(): self
    {
        return $this->only($this->scene['create'])
            ->append('config_key', 'unique:sys_config,config_key');
    }

}
